#include "Batch.h"

const char *Batch::EXT = ".mlb";
const char *Batch::DIR = "batches";
const char *Batch::OUTDIR = "results";

Batch::Batch() {
	instn = 0;
	instName = NULL;
	outName = NULL;
	ready = false;
}

Batch::~Batch() {
	if (instName != NULL)
		for (int i = 0; i < instn; i++)
			delete [] instName[i];

	if (outName != NULL)
		delete [] outName;
}

bool Batch::load( const char *name, Problem *problem ) throw (NotFound) {
	this->problem = problem;
	this->name = Util::copyArray(name, strlen(name) + 1);

	Input in(name);
	char str[128];

	// Read output file
	in.read(str);
	outName = new char[strlen(str) + 1];
	strcpy( outName, str );

	// Read instances
	in.read(execn);
	in.read(instn);
	instName = new char*[instn];

	for (int i = 0; i < instn; i++) {
		in.read(str);
		instName[i] = new char[strlen(str) + 1];
		strcpy( instName[i], str );
	}

	// Read configs
	in.read(cfgn);
	for (int c = 0; c < cfgn; c++) {
		loadConfig( in, c );
	}
	
	ready = true;
	return true;
}

void Batch::executeAll() throw (NotReady) {
	if (!ready)
		throw NotReady("Batch");

	for (int c = 0; c < cfgn; c++)
		executeConfig(c);
}

char* Batch::getPath() {
	char *path = new char[ strlen(name) + strlen(DIR) + strlen(EXT) + 1];
	sprintf(path, "%s/%s/%s%s", DIR, problem->getName(), name, EXT);
	return path;
}

char* Batch::getOutPath() {
	char *path = new char[ strlen(name) + strlen(OUTDIR) + 5];
	sprintf(path, "%s/%s/%s.csv", OUTDIR, problem->getName(), name);
	return path;
}
